package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import com.itheima.dto.ShoppingCarDto;
import com.itheima.mapper.ShoppingCarMapper;
import com.itheima.pojo.ShoppingCar;
import com.itheima.service.ShoppingCartService;
import com.itheima.thread.UserTokenThreadUtils;
import com.itheima.vo.ShoppingCarVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCarMapper shoppingCarMapper;

    @Override
    public List<ShoppingCar> getList() {
        List<ShoppingCar> list = shoppingCarMapper.getList();
        return list;
    }

    @Override
    public ShoppingCarVo add(ShoppingCarDto dto) {
        ShoppingCar shoppingCar = new ShoppingCar();
        BeanUtils.copyProperties(dto, shoppingCar);
        shoppingCar.setId(IdUtil.getSnowflakeNextIdStr());
        shoppingCar.setUserId(UserTokenThreadUtils.getThreadLocal().getId());
        shoppingCar.setCreateTime(LocalDateTime.now());
        //根据菜品ID或者套餐ID去查找购物车是否有这条数据,如果有则修改number值,否则新增
        ShoppingCar shoppingCarInDB=shoppingCarMapper.selectByDishIdOrSetmealId(shoppingCar);
        ShoppingCarVo shoppingCarVo = new ShoppingCarVo();
        if (shoppingCarInDB!=null){
            shoppingCarInDB.setNumber(shoppingCarInDB.getNumber()+1);
            Integer update=shoppingCarMapper.updatebyId(shoppingCarInDB);
            if (update>0){
                BeanUtils.copyProperties(shoppingCarInDB,shoppingCarVo);
                return shoppingCarVo;
            }
        }
        shoppingCar.setNumber(1);
        Integer insert=shoppingCarMapper.insertShoppingCar(shoppingCar);
        if (insert>0){
            BeanUtils.copyProperties(shoppingCar,shoppingCarVo);
            return shoppingCarVo;
        }
        return null;
    }

    @Override
    public ShoppingCarVo deleteShoppingCar(ShoppingCarDto shoppingCarDto) {
        ShoppingCar shoppingCar = new ShoppingCar();
        BeanUtils.copyProperties(shoppingCarDto,shoppingCar);
        shoppingCar.setId(IdUtil.getSnowflakeNextIdStr());
        shoppingCar.setUserId(UserTokenThreadUtils.getThreadLocal().getId());
        shoppingCar.setCreateTime(LocalDateTime.now());
        ShoppingCar shoppingCarInDB=shoppingCarMapper.selectByDishIdOrSetmealId(shoppingCar);
        ShoppingCarVo shoppingCarVo = new ShoppingCarVo();
        if (shoppingCarInDB.getNumber()>1){
            shoppingCarInDB.setNumber(shoppingCarInDB.getNumber()-1);
            Integer integer = shoppingCarMapper.updatebyId(shoppingCarInDB);
            if (integer>0){

                BeanUtils.copyProperties(shoppingCarInDB,shoppingCarVo);
                return shoppingCarVo;
            }
            return null;
        }
        Integer delete=shoppingCarMapper.deleteShoppingCar(shoppingCar);
        if (delete>0){
            BeanUtils.copyProperties(shoppingCarInDB,shoppingCarVo);
            shoppingCarVo.setNumber(0);
            return shoppingCarVo;
        }
        return null;
    }
    @Override
    public Boolean deleteAllShoppingCar() {
        Integer delete=shoppingCarMapper.deleteAllShoppingCar(UserTokenThreadUtils.getThreadLocal().getId());
        return delete>0;
    }
}
